<?php
require_once("includes/include.php");
header('Content-Type: text/xml');
echo '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>';
echo '<response>';
class blogKindManage extends daobase {
	function updateBlogKind($kindId,$newKind){
		@session_start();
		$userID = $_SESSION["userId"];
		$sql = "select id from blogkind where content='$newKind' and id !='$kindId' and userID ='$userID'";
		$handleUpdate = $this->fetchit($sql);
		$handleUpdate = $handleUpdate[0][0];
		if (!$handleUpdate){
			$sql ="update blogkind set content='$newKind' where id='$kindId'";
			$handle = $this->queryit($sql);
			return $handle;
		}else {
			return false;
		}		
	}
	function blogKindDlete($kindId){
		@session_start();
		$userID = $_SESSION["userId"];
		$sql = "select id from blogkind where content='默认' and userID='$userID'";
		$result = $this->fetchit($sql);
		if (empty($result)){
			$sql ="insert into blogkind(userID,content) values('$userID','默认')";
			$query = $this->queryit($sql);
			$sql = "select id from blogkind where content='默认' and userID='$userID'";
			$result = $this->fetchit($sql);
		}
		$morenKind = $result[0][0];
		$sql = "update blog set kind='$morenKind' where kind='$kindId'";
		$updateit= $this->queryit($sql);
		if($updateit){
			$sql = "delete from blogkind where id='$kindId'";
			$query =$this->queryit($sql);
			return $query;
		}else return false;
	}
}
$manageKind = trim($_POST["m"]);
//updat kind
if($manageKind==0){
	$newKind = trim($_POST["newKind"]);
	if($newKind==""){
		echo "<mes>kindEmpty</mes>";
	}else{
		$kindId = trim($_POST["kindId"]);
		$dao = new blogKindManage();
		$processUpdate =$dao->updateBlogKind($kindId,$newKind);
		if($processUpdate){
			echo "<mes>updateOk</mes>";
		}
		else {
			echo "<mes>updateFailed</mes>";
		}
	}
	
}
//delet kind
else if($manageKind==1){
	 $kindId = trim($_POST["kindId"]);
	 $dao = new blogKindManage();
	 $processUpdate =$dao->blogKindDlete($kindId);
	 if($processUpdate){
	 	echo "<mes>success</mes>";
	 }else {
	 	echo "<mes>failed</mes>";
	 }	 
}
else{
	return false;
}
echo '</response>';
?>